package cn.player.act.filter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.startchat.filter.filter.FilterManager;
import com.startchat.filter.image.ImageEglSurface;
import com.startchat.filter.image.ImageRenderer;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import cn.player.R;

public class ImageFilterActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView mImageView;
    private ImageRenderer mImageRenderer;
    private FilterTask mFilterTask;
    private LinearLayout mLlFilterImageArea;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_filter);

        mImageView = (ImageView) findViewById(R.id.image_view);
        mLlFilterImageArea = (LinearLayout) findViewById(R.id.ll_filter_image_area);

        findViewById(R.id.filter_normal).setOnClickListener(this);
        findViewById(R.id.filter_tone_curve).setOnClickListener(this);
        findViewById(R.id.filter_soft_light).setOnClickListener(this);
        findViewById(R.id.filter_blend).setOnClickListener(this);
        findViewById(R.id.filter_beauty).setOnClickListener(this);
        findViewById(R.id.filter_skin_white).setOnClickListener(this);
        findViewById(R.id.filter_antique).setOnClickListener(this);
        findViewById(R.id.filter_nostalgia).setOnClickListener(this);
        findViewById(R.id.filter_healthy).setOnClickListener(this);

        mImageRenderer =
                new ImageRenderer(getApplicationContext(), FilterManager.FilterType.Normal);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.filter_normal:
                startFilterTask(FilterManager.FilterType.Normal);
                break;
            case R.id.filter_tone_curve:
                startFilterTask(FilterManager.FilterType.ToneCurve);
                break;
            case R.id.filter_soft_light:
                startFilterTask(FilterManager.FilterType.SoftLight);
                break;
            case R.id.filter_blend:
                startFilterTask(FilterManager.FilterType.Blend);
                break;
            case R.id.filter_beauty:
                startFilterTask(FilterManager.FilterType.Beauty);
                break;
            case R.id.filter_skin_white:
                startFilterTask(FilterManager.FilterType.SkinWhite);
                break;
            case R.id.filter_antique:
                startFilterTask(FilterManager.FilterType.Antique);
                break;
            case R.id.filter_nostalgia:
                startFilterTask(FilterManager.FilterType.Nostalgia);
                break;
            case R.id.filter_healthy:
                startFilterTask(FilterManager.FilterType.Healthy);
                break;
        }
    }

    private void startFilterTask(FilterManager.FilterType filterType) {
        if (mFilterTask == null || mFilterTask.getStatus() != AsyncTask.Status.RUNNING) {
            mFilterTask = new FilterTask(getApplicationContext(), mImageRenderer, filterType);
            mFilterTask.execute();
        }
    }

    public class FilterTask extends AsyncTask<Void, Void, Bitmap> {

        private Context mContext;
        private FilterManager.FilterType mFilterType;
        private ImageRenderer mRenderer;

        public FilterTask(Context context, ImageRenderer renderer,
                          FilterManager.FilterType filterType) {
            mFilterType = filterType;
            mContext = context;
            mRenderer = renderer;
        }

        @Override
        protected Bitmap doInBackground(Void... params) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inScaled = false;
            final Bitmap bitmap =
                    BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bg_filter,
                            options);
            ImageEglSurface imageEglSurface =
                    new ImageEglSurface(bitmap.getWidth(), bitmap.getHeight()); //设置输出宽高,
            imageEglSurface.setRenderer(mRenderer);
            mRenderer.changeFilter(mFilterType);
            mRenderer.setImageBitmap(bitmap);
            imageEglSurface.drawFrame();
            Bitmap filterBitmap = imageEglSurface.getBitmap();
            imageEglSurface.release();
            mRenderer.destroy();


            saveToFile(filterBitmap, new File("/storage/emulated/0/tempvideo/"), "bg_filter_" + mFilterType + ".png");
            return filterBitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);

            if (bitmap == null) {
                return;
            }

//            mImageView.setImageBitmap(bitmap);
            addImage(bitmap);
        }
    }

    private int index = 0;

    private void addImage(Bitmap bitmap) {
        ImageView imageView = new ImageView(this);
        imageView.setImageBitmap(bitmap);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(250, 250);
        params.rightMargin = 20;
        mLlFilterImageArea.addView(imageView, index++, params);
    }


    public File saveToFile(Bitmap bitmap, File folder, String fileName) {
        if (bitmap != null) {
            if (!folder.exists()) {
                folder.mkdir();
            }
            File file = new File(folder, fileName);
            if (file.exists()) {
                file.delete();
            }
            try {
                file.createNewFile();
                BufferedOutputStream e = new BufferedOutputStream(new FileOutputStream(file));
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, e);
                e.flush();
                e.close();
                return file;
            } catch (IOException var5) {
                var5.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }
}
